﻿/**
 *	Copyright (c) 2009, SpLib.net
 * 	All rights reserved.
  	
 * 	Author:6spring
 * 	About:Socket连接编号
*/

package org.dm.net
{
	import flash.utils.ByteArray;
	
	public class GLID
	{
		public var m_nIP:uint;				//连接IP
		public var m_nTypePort:uint;			//连接地址信息，IP & Type
		public var m_nObjIDH:uint;			//对象ID高位
		public var m_nObjIDL:uint;			//对象ID低位
		
		public function GLID(source:Array=null)
		{
			if(source!=null)
			{
				m_nIP = source[0];
				m_nTypePort = source[1];
				m_nObjIDH = source[2];
				m_nObjIDL = source[3];
			}
			else
			{
				m_nIP = 0;
				m_nTypePort = 0;
				m_nObjIDH = 0;
				m_nObjIDL = 0;
			}
		}
		
		public function Clear():void
		{
			m_nIP = 0;
			m_nTypePort = 0;
			m_nObjIDH = 0;
			m_nObjIDL = 0;
		}
		
		public function SetAsArray(source:Array):void
		{
			m_nIP = source[0];
			m_nTypePort = source[1];
			m_nObjIDH = source[2];
			m_nObjIDL = source[3];
		}
		
		private function AddInt2Array(bytes:ByteArray,value:uint):void
		{
			var data:int;
			
			data = value & 0xFF;
			bytes.writeByte(data);
			
			data = (value>>8) & 0xFF;
			bytes.writeByte(data);
			
			data = (value>>16) & 0xFF;
			bytes.writeByte(data);
			
			data = (value>>24) & 0xFF;
			bytes.writeByte(data);
		}
		
		public function GetAsByteArray():ByteArray
		{
			var bytes:ByteArray = new ByteArray;
			AddInt2Array(bytes,m_nIP);
			AddInt2Array(bytes,m_nTypePort);
			AddInt2Array(bytes,m_nObjIDH);
			AddInt2Array(bytes,m_nObjIDL);
			return bytes;
		}
		
		public function IsEmpty():Boolean
		{
			if( m_nIP==0 || m_nTypePort==0)
			{
				return true;
			}
			
			if(m_nObjIDH==0 && m_nObjIDL==0)
			{
				return true;
			}
			
			return false;
		}
		
		public function GetAsArray():Array
		{
			return [m_nIP,m_nTypePort,m_nObjIDH,m_nObjIDL];
		}

		public function toString():String
		{
			return "GLID[" + m_nIP + "," + m_nTypePort + "," + m_nObjIDH + "," + m_nObjIDL + "]";
		}
		
		public function equal(rhs:GLID):Boolean
		{
			return m_nIP==rhs.m_nIP
					&& m_nIP==rhs.m_nTypePort
					&& m_nObjIDH==rhs.m_nObjIDH
					&& m_nObjIDL==rhs.m_nObjIDL;
		}

		static public function compare(rh1:GLID,rh2:GLID):int
		{
			if(rh1.m_nIP>rh2.m_nIP) return 1;
			if(rh1.m_nIP<rh2.m_nIP) return -1;
			
			if(rh1.m_nTypePort>rh2.m_nTypePort) return 1;
			if(rh1.m_nTypePort<rh2.m_nTypePort) return -1;
			
			if(rh1.m_nObjIDH>rh2.m_nObjIDH) return 1;
			if(rh1.m_nObjIDH<rh2.m_nObjIDH) return -1;
			
			if(rh1.m_nObjIDL>rh2.m_nObjIDL) return 1;
			if(rh1.m_nObjIDL<rh2.m_nObjIDL) return -1;
			
			return 0;
		}
	}
}